{% extends "cmdb/mbusiness/index.html" %}

{% block right_content %}
<div id="right_menu">
    <b>业务申请</b><hr>
    <form class="form-horizontal">
        <div class="form-inline" style="padding-left:37px">
            <label for="applier">申请人</label>
            <input class="form-control" style="width:400px;" type="text" id="applier" value="{{ request.session.username }}" disabled/>
            <span style="display:none;">*必填</span>
        </div><br />
        <div class="form-inline"  style="padding-left:15px">
            <label style="vertical-align: top;">部门经理<span style="color:#ff0000;">*</span></label>
            <input class="form-control" style="width:400px;" type="text" id="manager" placeholder="部门经理" />
            <span style="display:inline" id="warningDepartManger"></span>
        </div><br />
        <div class="form-inline" style="padding-left:29px">
            <label>模块名<span style="color:#ff0000;">*</span></label>
            <select class="form-control" id="business" onchange="subCategoryChange()">
                {% if category_info %}
                <option  value="{{ category_info.info.id }}" selected>{{ category_info.info.category_name }}</option>
                {% endif %}
                {% if category_list %}
                {% for idx,single_category in category_list.items %}
                <option  value="{{ idx }}">{{ single_category.info.category_name }}</option>
                {% endfor %}
                {% endif %}
            </select><span style="color:#ff0000;">*</span>
            <select class="form-control" id="develop" onchange="moduleUniqueCheck()" >
                {% if category_info %}
                {% for sub_category in category_info.sub_category %}
                <option  value="{{ sub_category.id }}">{{ sub_category.category_name }}</option>
                {% endfor %}
                {% endif %}
            </select><span style="color:#ff0000;">*</span>
            <input class="form-control" style="width:200px;" type="text" id="module" placeholder="模块名" onchange="moduleUniqueCheck()" />
            <span style="display:in-line;" id="warningModuleName"></span>
        </div><br />
        <div class="form-inline">
            <label style="vertical-align: top;">部署模块数<span style="color:#ff0000;">*</span></label>
            <input id="cutModuleNum" type="button" value="-"/>
            <input id="moduleNum" type="text" style=" width:50px; text-align:center" value="1"/>
            <input id="addModuleNum" type="button" value="+"/>
            <span style="display:in-line;" id="warningModuleNo"></span>
        </div><br />
        <div class="form-inline" style="padding-left:15px">
            <label style="vertical-align: top;">机器数量<span style="color:#ff0000;">*</span></label>
            <input id="cutMachineNum" type="button" value="-"/>
            <input id="machineNum" type="text" style=" width:50px; text-align:center" value="1"/>
            <input id="addMachineNum" type="button" value="+"/>
            <span style="display:in-line;" id="warningMachineNo"></span>
        </div><br />

        <div class="form-inline" style="padding-left:15px">
            <label style="vertical-align: top;">详细描述<span style="color:#ff0000;">*</span></label>
            <textarea rows="4" class="form-control" style="width:400px;" id="comment"  placeholder="详细描述"></textarea>
            <span style="display:in-line;" id="warningDesc"></span>
        </div><br />
    </form>
    <div style="padding-left: 100px;">
        <button type="button" class="btn btn-primary" id="item_add">
            提交
        </button>&nbsp;&nbsp;&nbsp;
        <button type="button" class="btn btn-default" id="add_cancel">
            取消
        </button>
    </div>
</div>

<script type="text/javascript">

$("#business").val({{ category_info.info.id }});
function subCategoryChange(){
    var firstCategory = Number(document.getElementById("business").value);
    category_info_id = {{ category_info.info.id }}
    jQuery("#develop").empty();
    if(firstCategory == category_info_id){
        {% for sub_category in category_info.sub_category %}
        jQuery("#develop").append("<option value='{{ sub_category.id }}'>{{ sub_category.category_name }}</option>");
        {% endfor %}
    }else{
        {% for id,category in category_list.items %}
            var category_info_id = {{ id }}
            if(category_info_id == firstCategory){
                {% for sub_category in category.sub_category %}
                jQuery("#develop").append("<option value='{{ sub_category.id }}'>{{ sub_category.category_name }}</option>");
                {% endfor %}
            }
        {% endfor %}
    }
    moduleUniqueCheck();
}
//判断模块唯一性
function moduleUniqueCheck(){
    var moduleName =  $("#module").val();
    if(moduleName == ''){
         $("#warningModuleName").html("");
        return true;
    }
    var chechStatus = true;
    var category =  $("#develop").val();
    var urls = "{% url 'business:muniqueaction' %}";
    var postdata = {category:category,module_name:moduleName};
    var jsonstr = $.ajax({
			url : urls,
			type : "POST",
			data : postdata,
			async : false,
			cache : false
		});
	var jsonobj = eval('(' + jsonstr.responseText + ')');
	if(jsonobj.status == 1){
	    if(jsonobj.data == true){
	        if(jsonobj.module_status == 4){
	            $("#warningModuleName").html("<font size='2' style='color:red'>模块已存在</font>");
	        }else if (jsonobj.module_status == 1){
	            $("#warningModuleName").html("<font size='2' style='color:red'>模块已申请</font>");
	        }
	        chechStatus = false;
	    }else{
	        $("#warningModuleName").html("");
	    }
	}else{
	    chechStatus = false;
	}
	return chechStatus;
}

function nullValueCheck(value,attr){
    if(value == ''){
        $("#" + attr).html("<font size='2' style='color:red'>不能为空</font>");
        return false;
    }
    if((attr=='warningModuleNo' || attr=='warningMachineNo') && value == 0){
       $("#" + attr).html("<font size='2' style='color:red'>不能为零</font>");
        return false;
    }
    $("#" + attr).html("");
    return true;
}
$("#item_add").click(function(){
    var applier =  $("#applier").val();
    var manager =  $("#manager").val();
    var business =  $("#business").val();
    var develop =  $("#develop").val();
    var module =  $("#module").val();
    var moduleNum =  $("#moduleNum").val();
    var machineNum =  $("#machineNum").val();
    var comment =  $("#comment").val();
    if(!nullValueCheck(manager,'warningDepartManger') || !nullValueCheck(module,'warningModuleName')
       || !nullValueCheck(moduleNum,'warningModuleNo') || !nullValueCheck(machineNum,'warningMachineNo')
       || !nullValueCheck(comment,'warningDesc') || !moduleUniqueCheck()){
        return;
    }
    var urls = "{% url 'business:mapplycation' %}";
    var postdata = {applier:applier,depart_manger:manager,category:develop,parent_category:business,module_name:module,
                   module_number:moduleNum,apply_server_number:machineNum,description:comment}
    var jsonstr = $.ajax({
			url : urls,
			type : "POST",
			data : postdata,
			async : false,
			cache : false
		});
	var jsonobj = eval('(' + jsonstr.responseText + ')');
	if(jsonobj.status == 0){
	    alert(jsonobj.message);
	}else{
	    window.location.href="{% url 'business:mapplyinfo' %}";
	}

});
$("#add_cancel").click(function(){
    window.location.href="{% url 'business:mapplyinfo' %}";s
});

var mNum=0;
$("#cutModuleNum").click(function(){
mNum=$("#moduleNum").val();
if(mNum==0){
return
}
else{
mNum--;
}
$("#moduleNum").val(mNum);
});
$("#addModuleNum").click(function(){
mNum=$("#moduleNum").val();
mNum++;
$("#moduleNum").val(mNum);
});

var maNum = 0;
$("#cutMachineNum").click(function(){
maNum=$("#machineNum").val();
if(maNum==0){
return
}else{
maNum--;
}
$("#machineNum").val(maNum);
});
$("#addMachineNum").click(function(){
maNum=$("#machineNum").val();
maNum++;
$("#machineNum").val(maNum);
});
</script>
{% endblock %}